package com.idnx.mynewsdemo.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.idnx.mynewsdemo.entity.NewsBean;

import java.util.ArrayList;
import java.util.List;

public class DataBaseBean extends SQLiteOpenHelper {
    private static final String DATA_NAME = "newsBean.db";
    private static final int DATA_VERSION=1;
    private static final String TABLE_NAME = "users";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_USERNAME="username";
    private static final String COLUMN_PASSWORD = "password";

    private static final String TABLE_NAME_CLICKS = "user_news_clicks";
    private static final String COLUMN_CLICK_ID = "click_id";
    private static final String COLUMN_NEWS_ID = "news_id";
    private static final String COLUMN_CLICK_TIME = "click_time";
    private static final String COLUMN_URL = "url";
    private static final String COLUMN_NEWS_NAME = "news_name";
    public DataBaseBean(@Nullable Context context) {
        super(context, DATA_NAME, null, DATA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_USERNAME + " TEXT UNIQUE, "+
                COLUMN_PASSWORD + " TEXT)");
        db.execSQL("CREATE TABLE " + TABLE_NAME_CLICKS + " (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_USERNAME + " TEXT, " +
                COLUMN_NEWS_ID + " TEXT, " +
                COLUMN_CLICK_TIME + " TEXT, " +
                COLUMN_URL + " TEXT, " +
                COLUMN_NEWS_NAME + " TEXT, " +
                "UNIQUE(" + COLUMN_USERNAME + ", " + COLUMN_NEWS_ID + ") ON CONFLICT REPLACE)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_CLICKS);
        onCreate(db);
    }

    public boolean registerUser(String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, username);
        values.put(COLUMN_PASSWORD, password);
        long result = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        db.close();
        return result != -1;
    }
    public boolean login(String username,String password) {
        SQLiteDatabase database = this.getReadableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_USERNAME + " = ? AND " +
                COLUMN_PASSWORD + " = ?";
        Cursor cursor = database.rawQuery(query, new String[]{username, password});
        boolean result = cursor.getCount() >0;
        cursor.close();
        database.close();
        return result;
    }
    public void saveOrUpdateClickRecord(String username, String newsId, String clickTime, String url, String newsName) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, username);
        values.put(COLUMN_NEWS_ID, newsId);
        values.put(COLUMN_CLICK_TIME, clickTime);
        values.put(COLUMN_URL, url);
        values.put(COLUMN_NEWS_NAME, newsName);
        db.insertWithOnConflict(TABLE_NAME_CLICKS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        db.close();
    }

    @SuppressLint("Range")
    public List<NewsBean.ResultBean.DataBean> getUserClickRecords(String username) {
        List<NewsBean.ResultBean.DataBean> clickRecords = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME_CLICKS,
                new String[]{COLUMN_NEWS_ID, COLUMN_CLICK_TIME, COLUMN_URL, COLUMN_NEWS_NAME},
                COLUMN_USERNAME + " = ?",
                new String[]{username},
                null, null, COLUMN_CLICK_TIME + " DESC");

        if (cursor != null) {
            while (cursor.moveToNext()) {
                String newsId = cursor.getString(cursor.getColumnIndex(COLUMN_NEWS_ID));
                String clickTime = cursor.getString(cursor.getColumnIndex(COLUMN_CLICK_TIME));
                String url = cursor.getString(cursor.getColumnIndex(COLUMN_URL));
                String newsName = cursor.getString(cursor.getColumnIndex(COLUMN_NEWS_NAME));
                NewsBean.ResultBean.DataBean newsBean = new NewsBean.ResultBean.DataBean();
                newsBean.setTitle(newsName);
                newsBean.setDate(clickTime);
                newsBean.setUniquekey(newsId);
                newsBean.setUrl(url);
                clickRecords.add(newsBean);
            }
            cursor.close();
        }

        db.close();
        return clickRecords;
    }

}
